package com.thib.htstvremote;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;



public class MainActivity extends Activity  {
	private String[] mPlanetTitles;
	private ListView mDrawerList;
	private DrawerLayout mDrawerLayout;
	private ActionBarDrawerToggle mDrawerToggle;
	public Requests r;
	private CharSequence mTitle;
	private CharSequence mDrawerTitle;
	private Fragment fragment;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mTitle = mDrawerTitle = getTitle();
		
		mPlanetTitles = getResources().getStringArray(R.array.planets_array);
		mDrawerList = (ListView) findViewById(R.id.left_drawer);
		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description */
                R.string.drawer_close  /* "close drawer" description */
                ) {

            /** Called when a drawer has settled in a completely closed state. */
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); 
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu();
            }
        };
 
		 
		 
		 
        // Set the drawer toggle as the DrawerListener
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);
        mDrawerLayout.setDrawerListener(mDrawerToggle);
		 
        
        
		// Set the adapter for the list view
        mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_list_item, mPlanetTitles));
        
     // Set the list's click listener
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 
        
        r = new Requests();
       
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		
		return true;
	}
	
	
	
	 @Override
	    protected void onPostCreate(Bundle savedInstanceState) {
	        super.onPostCreate(savedInstanceState);
	        // Sync the toggle state after onRestoreInstanceState has occurred.
	        mDrawerToggle.syncState();
	    }

	    @Override
	    public void onConfigurationChanged(Configuration newConfig) {
	        super.onConfigurationChanged(newConfig);
	        mDrawerToggle.onConfigurationChanged(newConfig);
	    }
	
	    @Override
	    public boolean onOptionsItemSelected(MenuItem item) {
	        // Pass the event to ActionBarDrawerToggle, if it returns
	        // true, then it has handled the app icon touch event
	        if (mDrawerToggle.onOptionsItemSelected(item)) {
	          return true;
	        }
	        // Handle your other action bar items...

	        return super.onOptionsItemSelected(item);
	    }

	/* The click listner for ListView in the navigation drawer */
    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
    }
    
    /** Swaps fragments in the main content view */
    private void selectItem(int position) {
        // Create a new fragment and specify the planet to show based on position
        fragment = new ItemFragment();
        Bundle args = new Bundle();
        args.putInt(ItemFragment.ARG_ITEM_NUMBER, position);
        fragment.setArguments(args);
        // Insert the fragment by replacing any existing fragment
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                       .replace(R.id.content_frame, fragment)
                       .commit();
        // Highlight the selected item, update the title ,then close the drawer
        mDrawerList.setItemChecked(position, true);
        setTitle(mPlanetTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
        ((ItemFragment)fragment).b = false;
        switch (position) {
        case 0:
        	
        	 new Thread(new Runnable() {
        		    public void run() {r.getChannels();
        		    runOnUiThread(new Runnable() {
        		        public void run() {
        		        	((ItemFragment)fragment).MysetAdap(r.entrieList);
        		        	
        		        }});
        		    
        		    }}).start();
        	//args.putParcelableArrayList(ItemFragment.ARG_ITEM_LST, r.entrieList);
            break;
        case 1:
        	 new Thread(new Runnable() {
     		    public void run() {r.getChannelsTags();
     		   runOnUiThread(new Runnable() {
     			   public void run() {
     				   ((ItemFragment)fragment).MysetAdap(r.entrieList);
   		        	
     			   }});
     		    }}).start();
        	//args.putParcelableArrayList(ItemFragment.ARG_ITEM_LST, r.entrieList);
            break;
        case 2:
       	 new Thread(new Runnable() {
    		    public void run() {r.getUpcoming();
    		   runOnUiThread(new Runnable() {
    			   public void run() {
    				   ((ItemFragment)fragment).MysetAdap2(r.arrayRecorder);
  		        	
    			   }});
    		    }}).start();
       	//args.putParcelableArrayList(ItemFragment.ARG_ITEM_LST, r.entrieList);
           break;
        case 3:
       	 new Thread(new Runnable() {
    		    public void run() {r.getFinished();
    		   runOnUiThread(new Runnable() {
    			   public void run() {
    				   ((ItemFragment)fragment).MysetAdap2(r.arrayRecorder);
  		        	
    			   }});
    		    }}).start();
       	//args.putParcelableArrayList(ItemFragment.ARG_ITEM_LST, r.entrieList);
           break;
        case 4:
          	 new Thread(new Runnable() {
       		    public void run() {r.getFailed();
       		   runOnUiThread(new Runnable() {
       			   public void run() {
       				   ((ItemFragment)fragment).MysetAdap2(r.arrayRecorder);
     		        	
       			   }});
       		    }}).start();
          	//args.putParcelableArrayList(ItemFragment.ARG_ITEM_LST, r.entrieList);
              break;
        case 5:
        	((ItemFragment)fragment).b = true;
         	 new Thread(new Runnable() {
      		    public void run() {r.getEpg(0);
      		   runOnUiThread(new Runnable() {
      			   public void run() {
      				   ((ItemFragment)fragment).MysetAdap2(r.arrayRecorder);
    		        	
      			   }});
      		    }}).start();
         	//args.putParcelableArrayList(ItemFragment.ARG_ITEM_LST, r.entrieList);
             break;
        case 6:
        	//args.putParcelableArrayList(ItemFragment.ARG_ITEM_LST, r.entrieList);
            break;
        }

    }
    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getActionBar().setTitle(mTitle);
    }
}
